/**
 * Mscratch.import加载文件的相对路径
 * 如果使用绝对路径，则使用原生的importScript方法
 * eg: importScript('https://170.0.0.1:8502/static/extensions/demo/sensorium.js')
 */
Mscratch.import('/demo/sensorium.js');
const sensorium = new Sensorium();
const mcore = sensorium.createMcore();

const Directions = {
    forward: 'forward',
    backward: 'backward',
    left: 'left',
    right: 'right'
}

function McoreExtension() {
}

McoreExtension.prototype.getInfo = function () {
    const {TargetTypes, BlockType, ArgumentType} = Mscratch;
    return {
        // 扩展的唯一标识
        id: 'workerDemo',
        // 扩展名
        name: 'Ext',
        // 扩展描述
        description: 'A demo of worker extension',
        // 扩展图标
        iconURI: '' +
        'UIMBgAEWB7i7uidhAAAAABJRU5ErkJggg==',
        // 扩展文档
        docsURI: 'https://...',
        // 扩展支持的目标类型
        targets: null,
        // categories
        colours: {
            color1: '#FF6680',
            color2: '#FF4D6A',
            color3: '#FF3355'
        },
        // blocks
        blocks: [
            {
                // block 的标识
                opcode: 'demo_move',
                // block 的类型
                // 'command' - a normal command block, like "move {} steps"
                // 'reporter' - returns a value, like "direction"
                // 'Boolean' - same as 'reporter' but returns a Boolean value
                // 'hat' - starts a stack if its value is truthy
                // 'conditional' - control flow, like "if {}" or "repeat {}"
                blockType: BlockType.COMMAND,
                text: 'workerDemo.demo_move',
                arguments: {
                    DIRECTION: {
                        type: ArgumentType.STRING,
                        menu: 'directionMenu',
                        defaultValue: Directions.forward
                    },
                    POWER: {
                        type: ArgumentType.NUMBER,
                        defaultValue: 50
                    }
                },
                func: 'moveWithTime',
                codes: {
                    python: {
                        imports: ['demo'],
                        code: 'mcar.%1(%2, %3)'
                    }
                }
            }
        ],
        // 菜单选项
        menus: {
            directionMenu: [
                {
                    value: Directions.forward,
                    text: 'workerDemo.forward'
                },
                {
                    value: Directions.backward,
                    text: 'workerDemo.backward'
                },
                {
                    value: Directions.left,
                    text: 'workerDemo.left'
                },
                {
                    value: Directions.right,
                    text: 'workerDemo.right'
                }
            ]
        },
        // 多语言支持
        transition_map: {
            en: {
                demo_move: '[DIRECTION] at power [POWER]',
                forward: 'forward',
                backward: 'back',
                left: 'left',
                right: 'right'
            },
            zh: {
                demo_move: '[DIRECTION] 动力 [POWER]',
                forward: '前进',
                backward: '后退',
                left: '左转',
                right: '右转'
            }
        }
    };
}

McoreExtension.prototype.moveWithTime = function (args) {
    let duration = Number(args.DURATION) * 1000;
    let direction = args.DIRECTION;
    let speed = Number(args.POWER);
    const DIRECTION2SPEED = {
        'forward': [-1, 1],
        'back': [1, -1],
        'left': [1, 1],
        'right': [-1, -1]
    };
    let leftDir = DIRECTION2SPEED[direction][0];
    let rightDir = DIRECTION2SPEED[direction][1];
    let protocol = mcore.VirtualJoystick()
        .leftSpeed(leftDir * speed)
        .rightSpeed(rightDir * speed)
        .protocol();

    Mscratch.remote.sendProtocol(protocol, false).then(function () {
        setTimeout(function () {
            protocol = mcore.VirtualJoystick().stop().protocol();
            Mscratch.remote.sendProtocol(protocol);
        }, duration);
    });
}

// 注册扩展，主进程将对扩展进行加载
Mscratch.register(new McoreExtension());
